//
//  SearchViewController.m
//  TipsProject
//
//  Created by Suny on 15/5/1.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "SearchViewController.h"
#import "MGBox.h"
#import "TopicInfoService.h"
#define IPHONE_PORTRAIT_GRID   (CGSize){self.view.frame.size.width, 0}
#define IPHONE_PORTRAIT_PHOTO  (CGSize){(self.view.frame.size.width-(kColumCountPerRow + 1)*kItemInterval)/kColumCountPerRow, 148}
#define kColumCountPerRow 2
#define kItemInterval 8
#import "TopicView.h"
#import "SubTopicViewcontroller.h"
#import "TipAbstractListViewController.h"

@interface SearchViewController ()

@property (nonatomic, strong) MGBox *photosGrid;
@property (nonatomic, strong) NSMutableArray *tipItemArray;

@property (nonatomic, strong) TopicInfoService *service;

@end

@implementation SearchViewController

- (TopicInfoService *)service
{
	if (!_service)
	{
		_service = [TopicInfoService serviceWithDefaultBaseUrlString];
	}
	return _service;
}
- (id)init
{
	self = [super init];
	if (self)
	{
		self.hasNavigationBar = YES;
	}
	return self;
}


- (NSMutableArray *)tipItemArray
{
	if (!_tipItemArray)
	{
		_tipItemArray = [NSMutableArray array];
	}
	return _tipItemArray;
}
- (MGScrollView *)scroller
{
	if (!_scroller)
	{
		_scroller = [MGScrollView scroller];
	}
	
	return _scroller;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self setNavigationTitle:@"发现"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"ebebeb"];

	self.scroller.frame = CGRectMake(0, 
									 self.navigationBarHeight, 
									 self.view.width, 
									 self.view.height - self.navigationBarHeight - 44);
	
	[self.view addSubview:self.scroller];
    self.scroller.backgroundColor = [UIColor colorWithHexString:@"ebebeb"];
	
	// i'll be using this a lot
	
	// setup the main scroller (using a grid layout)
	self.scroller.contentLayoutMode = MGLayoutStackHorizontalWithWrap;
	self.scroller.bottomPadding = 8;
	
	// iPhone or iPad grid?
	CGSize photosGridSize = IPHONE_PORTRAIT_GRID;
	
	// the photos grid
	_photosGrid = [MGBox boxWithSize:photosGridSize];
	_photosGrid.contentLayoutMode = MGLayoutStackHorizontalWithWrap;
	
	[self.scroller.boxes addObject:_photosGrid];
	[_photosGrid layout];
	
//	TopicView *topicView = [TopicView topicViewWithTopic:self.topicInfo
//											   widthSize:[self photoBoxSize]];
//	[_photosGrid.boxes addObject:topicView];
//	topicView.title = self.tipTitle;
//	
//	topicView.onTap = ^{
//		
//		
//	};
//	
//	// add a blank "add photo" box
//	[_photosGrid.boxes addObject:self.photoAddBox];
	
	[self requestDataFromNet];

}



- (void)requestDataFromNet
{
	[self.coverView startAnimating];
	
	WEAKSELF
	[self.service getTopicListWithFinishBlock:^(BaseResponse *response)
	 {
		 STRONGSELF
		 if(response.success)
		 {
			 strongSelf.tipItemArray = [NSMutableArray arrayWithArray:response.object];

			 for (TopicTableDisplayInfo *info in strongSelf.tipItemArray)
			 {
				 TopicView *view = [TopicView topicViewWithTopic:info.topicInfo
													   widthSize:IPHONE_PORTRAIT_PHOTO];
				 
				 __weak TopicView *weakBox = view;
				 view.bigStyle = YES;
				 view.onTap = ^{
					 [strongSelf clickView:weakBox.topicInfo];
				 };
				 [self.photosGrid.boxes addObject:view];
				 
			 }
			 
			 [self.scroller layoutWithSpeed:0.3 completion:nil];
			 [strongSelf.coverView stopAnimating];
		 }
		 else
		 {
			 
			 [strongSelf showErrorMessage:response.desc];
			 [strongSelf.coverView stopAnimationWithRetryAction:@selector(requestDataFromNet)
                                                  withActObject:strongSelf];
			 
		 }
	 }];
}



- (void)clickView:(TopicInfo *)info
{
	DLogObject(info);

    if (CHECK_VALID_ARRAY(info.subTopic))
    {
        SubTopicViewcontroller *subTopicViewcontroller = [[SubTopicViewcontroller alloc] init];
        subTopicViewcontroller.viewForSearch = YES;
		subTopicViewcontroller.hasNavigationBar = YES;
		[subTopicViewcontroller setNavigationTitle:info.name];

        NSMutableArray *displayInfoArray = [NSMutableArray array];
        for (TopicInfo *subTopic in info.subTopic)
        {
            TopicTableDisplayInfo *displayInfo = [[TopicTableDisplayInfo alloc] initWithModelInfo:subTopic];
            [displayInfo setStyle:TopicTableViewCellContentSubStyle1];
            [displayInfoArray addObject:displayInfo];

        }
        subTopicViewcontroller.modelArray = displayInfoArray;

        [[AppDelegate rootNavigationController] pushViewController:subTopicViewcontroller
                                             animated:YES];
    }
	else
	{
		TipAbstractListViewController *abstractListViewController = [[TipAbstractListViewController alloc] initWithSlideInfo:nil];
        abstractListViewController.topicInfo = info;
		abstractListViewController.hasNavigationBar = YES;
		abstractListViewController.title = info.name;
        [[AppDelegate rootNavigationController] pushViewController:abstractListViewController
                                                          animated:YES];
    }
	
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
